<template>
  <div class="elm-edit f_edit">
    <elm-page-bb class="ei_edit" v-loading="loading">

      <el-form ref="model" :model="model" size="small" label-width="100px">
        <elm-row>
          <el-form-item label="名称" prop="param_name" :rules="[{ required: true, message: '名称 必须填写'}]">
            <el-input v-model="model.param_name"></el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="说明" prop="param_explain">
            <el-input type="textarea" :rows="2" v-model="model.param_explain"></el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="默认值" prop="param_value">
            <el-input v-model="model.param_value"></el-input>
          </el-form-item>
        </elm-row>
      </el-form>

      <template #bottom>
        <el-button type="success" size="small" @click="submit">立即保存</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </template>
    </elm-page-bb>
  </div>
</template>

<script>
export default {
  extends: elmVueBase,
  data() {
    return {
      loading:true,

      DE_INFO: null,
      edit_type: '',

      flow_model:{},
      flowExist:false,

      model: {},
    }
  },
  mounted() {
    this.DE_INFO = elm.dialog.request;
    this.edit_type = this.DE_INFO.edit_type;
    this.flow_model = this.DE_INFO.flow_model;

    this.v_getData();
  },
  methods: {
    async v_getData(){
      this.loading = true;

      if(this.edit_type=='create'){
        this.model = {
          'id': elm.newGuid(),
          'flow_id': this.flow_model.id,
        };
      }
      else{
        this.flowExist = true;
        this.model = this.DE_INFO.model;
      }

      //如果流程尚未入库，那么先保存流程
      let check = await elm.post('/system/login/flow/GetFlowList', { "id": this.flow_model.id});
      if (check.length>0) {
        this.flowExist = true;
        //this.DE_INFO.callVue.setFLowExist(true);
      }
      else{
        if (this.em(this.flow_model.title)){this.$message.error('缺少流程名称'); this.v_cancle();return;}
        if (this.em(this.flow_model.code)){
          let cData = await elm.post('/system/login/flow/GetFlowCodeForCreate', {});
          if(this.em(cData.code) || cData.code.indexOf('flow_')<0){
            this.$message.error('流程编码生成失败，请稍后尝试。');
            this.loading = false;
            return;
          }
          this.flow_model.code = cData.code;
        }
        if(this.em(this.flow_model.order_number)){this.flow_model.order_number = this.flow_model.code;}
        let check = await elm.post('/system/login/flow/GetFlowList', { "code": this.flow_model.code});
        if (check.length>0 && check[0].id !=this.flow_model.id) { this.$message.error('流程编码重复'); this.v_cancle();return;}

        if(!this.em(this.flow_model.secForm_tableName)){
          this.flow_model['ff_tablename'] = 'biz_'+this.flow_model.secForm_tableName;
        }
        else{ this.flow_model['ff_tablename'] = 'biz_'+this.flow_model.code; }

        this.flow_model['on_canvas'] = 'yes';
        let saveFlag = await elm.post('/system/login/flow/UpserFlowAndSecForm', {'model':this.flow_model});
        if (saveFlag==false){this.$message.error('流程保存失败'); this.v_cancle();return;}

        this.DE_INFO.callVue.setFLowExist(true);
      }

      this.loading = false;
    },

    async submit() {
      try{await this.$refs["model"].validate();}catch{console.log('表单验证未通过'); return;}

      this.loading = true;

      this.model['flow_id'] = this.flow_model.id;
      let result = await elm.post('/system/login/flow/UpsertFlowParam', this.model);
      if( result==false ){ this.$message.error('流程参数保存失败'); }
      else { this.$message.success('流程参数已保存'); }

      this.DE_INFO.callVue.v_getParamList();
      this.v_cancle();
    },
    v_cancle:function (){
      this.close();//extends: elmVueBase
    },

    em: function (s) {
      if (s == null || s == '') { return true; }
      else { return false; }
    },

  }
}
</script>
<style>
.f_edit .elm-body{padding: 5px;}
</style>